package com.share.service.common;

import com.share.common.error.BizException;
import com.share.common.error.JsonReturnCode;
import com.share.pojo.converter.DtoConverter;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 基类service
 *
 * @author liw
 * @version 1.0
 * @date 2022/6/27 10:51
 */
@Service
@Slf4j
public class BaseService {

    @Autowired
    private HttpSession session;

    @Autowired
    protected DtoConverter dtoConverter;


    /**
     * 管理員的id=1,所以为1的不可修改
     *
     * @param id 管理員的id
     */
    protected void checkAdmin(Long id) {
        if (id != null && id == 1) {
            throw new BizException(JsonReturnCode.ADMIN_NO_UPDATE);
        }
    }

    /**
     * 获取请求信息
     *
     * @return
     */
    private ServletRequestAttributes getRequestAttributes() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes;
    }

    /**
     * 获取request
     *
     * @return
     */
    protected HttpServletRequest request() {
        return this.getRequestAttributes().getRequest();
    }

    /**
     * 获取response
     *
     * @return
     */
    protected HttpServletResponse response() {
        return this.getRequestAttributes().getResponse();
    }

    /**
     * 获取输出流
     *
     * @param contentType 返回前台的文件类型
     * @return
     */
    protected OutputStream getOutPutStream(ContentType contentType) {
        HttpServletResponse response = this.response();
        response.setContentType(contentType.toString());
        try {
            return response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BizException(JsonReturnCode.IO_ERROR);
        }
    }

    /**
     * 服务地址
     *
     * @return
     */
    protected String serverPath() {
        String url = request().getRequestURL().toString();
        String uri = request().getRequestURI();
        url = url.replace(uri, "");
        return url;
    }

    /**
     * 服务地址
     *
     * @return
     */
    protected String requestUri() {
        return request().getRequestURI();
    }

    protected String getTenantIdStr() {
        String tenantid = request().getHeader("tenantid");
        log.info("----getTenantIdStr-----the getTenantIdStr is {}", tenantid);
        return tenantid;
    }

    protected String getTenantId() {
        String tenantId = request().getHeader("tenantId");
        if (tenantId == null) {
            return "xy";
        }
        return tenantId;

    }


}
